﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Employees;

namespace VK.Casemeet.Web.Admin.Employees
{
    public partial class EmployeeEdit : System.Web.UI.Page
    {
        public int employeeID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            employeeID = Util.GetQueryInt("EmployeeID");

            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="管理员列表",Url="/Admin/Employees/EmployeeList.aspx"},
                new MapPath{ Text="编辑管理员",Url="#"}
            };

            if (!IsPostBack)
            {
                EmployeeHelper.BindRoleDDL(ddlRoleID, OptionType.Select);
                LoadData();
            }
        }

        internal void LoadData()
        {
            //编辑时
            trPassword.Visible = false;
            trCheckPassword.Visible = false;
            EmployeeEntity employee = EmployeeBLL.GetEmployee(employeeID);
            ddlRoleID.SelectedValue = employee.RoleID.ToString();
            txtAccount.Text = employee.Account;
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            EmployeeEntity employee = LoadEntity();
            employee.EmployeeID = employeeID;

            if (EmployeeBLL.UpdateEmployee(employee) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected EmployeeEntity LoadEntity()
        {
            EmployeeEntity employee = new EmployeeEntity();
            employee.RoleID = int.Parse(ddlRoleID.SelectedValue);
            employee.Account = txtAccount.Text;
            return employee;
        }
    }
}